package com.macro.mall.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.macro.mall.common.api.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author: Bernard
 * @date: 2022/7/12 3:48
 * @description:
 */
public class RateLimitController {
    /**
     * 按资源名称限流，需要指定限流处理逻辑
     */
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource() {
        return CommonResult.success("按资源名称限流");
    }

    /**
     * 按URL限流，有默认的限流处理逻辑
     */
    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl",blockHandler = "handleException")
    public CommonResult byUrl() {
        return CommonResult.success("按url限流");
    }

    public CommonResult handleException(BlockException exception){
        return CommonResult.success(exception.getClass().getCanonicalName());
    }
}

